package com.qigao.mall.channel.alipay;

import com.alibaba.fastjson.JSONObject;
import com.qigao.mall.channel.ChannelCore;
import com.qigao.mall.channel.dto.ChannelPayRespDto;
import com.qigao.mall.commons.dto.MerChannelDto;
import com.qigao.mall.commons.enums.*;
import com.qigao.mall.commons.param.ChannelReqParam;
import com.qigao.mall.commons.param.ChannelRespParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author:luiz
 * @Date: 2018/5/28 11:16
 * @Descripton:支付宝条码支付
 * @Modify : 当面付产品介绍 https://docs.open.alipay.com/194
 * 条码付接口文档：https://docs.open.alipay.com/api_1/alipay.trade.pay
 **/
@Service
public class AliPayTiaoMaPay extends AbstractAliPay implements ChannelCore {
    private static final Logger logger= LoggerFactory.getLogger(AliPayTiaoMaPay.class);

    @Override
    public boolean isSupport(PayProductEnum payProductEnum, ChannelActionEnum channelActionEnum, ChannelInfoEnum channelInfoEnum) {
        return ChannelActionEnum.CREATE_ORDER.equals(channelActionEnum) &&
                PayProductEnum.ALIPAY_TIAOMA.equals(payProductEnum) &&
                ChannelInfoEnum.ALIPAY.equals(channelInfoEnum);
    }

    @Override
    protected String getTradeMethod() {
        return "alipay.trade.pay";
    }


    @Override
    public Map buildSelfReqParam(MerChannelDto merChannelDto, ChannelReqParam channelReqParam) {
        Map reqParam=new ConcurrentHashMap<>();
        JSONObject bizContent=new JSONObject();
        bizContent.put("out_trade_no", merChannelDto.getInnerSeqNo());
        bizContent.put("scene", "bar_code");//支付场景 条码支付，取值：bar_code 声波支付，取值：wave_code
        bizContent.put("auth_code","");//todo 支付授权码，25~30开头的长度为16~24位的数字，实际字符串长度以开发者获取的付款码长度为准
        bizContent.put("product_code","FACE_TO_FACE_PAYMENT");
        JSONObject reqContent= JSONObject.parseObject(channelReqParam.getReqContent());
        bizContent.put("subject",reqContent.getString("goodsName"));
        bizContent.put("total_amount",reqContent.getString("payAmt"));
        bizContent.put("body",reqContent.getString("goodsName"));
        reqParam.put("biz_content",bizContent.toString());
        return reqParam;
    }

    @Override
    protected ChannelRespParam buildSelfRespData(JSONObject respJson, MerChannelDto merChannelDto) {
        ChannelRespParam channelRespParam=new ChannelRespParam();
        channelRespParam.setRespCode(ChannelCodeEnum.SUCCESS.getCode());
        channelRespParam.setRespMsg(ChannelCodeEnum.SUCCESS.getName());
        String code=respJson.getString("code");
        String msg=respJson.getString("msg");
        ChannelPayRespDto channelPayRespDto =new ChannelPayRespDto();
        channelPayRespDto.setIfSyn(YesNoEnum.YES);
        channelPayRespDto.setRetCode(code);
        channelRespParam.setRespMsg(msg);
        if("40004".equals(code)){
            logger.info("【支付宝查询交易】接口调用业务失败:{}",respJson);
            channelRespParam.setRespCode(ChannelCodeEnum.SUCCESS.getCode());
            channelPayRespDto.setThirdTradeNo(respJson.getString("trade_no"));
            String subCode=respJson.getString("sub_code");
            String subMsg=respJson.getString("sub_msg");
            if("ACQ.TRADE_NOT_EXIST".equals(subCode)){//付款不存在则关闭
                channelPayRespDto.setTradeStatus(PayStatusEnum.PAY_INIT);
            }
            channelPayRespDto.setRetCode(subCode);
            channelPayRespDto.setRetMsg(subMsg);
            channelPayRespDto.setOrignRespContent(respJson);
            channelRespParam.setRespContent(JSONObject.toJSONString(channelPayRespDto));
            return channelRespParam;
        }
        channelPayRespDto.setThirdTradeNo(respJson.getString("trade_no"));
        channelPayRespDto.setInnerSeqNo(respJson.getString("out_trade_no"));
        channelPayRespDto.setBuylerId(respJson.getString("buyer_logon_id"));
        channelPayRespDto.setTradeStatus(PayStatusEnum.PAY_SUCCESS);
        String totalAmt=respJson.getString("total_amount");
        channelPayRespDto.setTradeAmt(new BigDecimal(totalAmt));
        channelPayRespDto.setPaySuccessTime(new Date());
        channelRespParam.setRespContent(JSONObject.toJSONString(channelPayRespDto));
        return channelRespParam;
    }


}
